package org.szy.plugin.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.ProxyComponent;
import org.szy.core.common.enums.CommonStatusEnum;
import org.szy.plugin.sys.mapper.SysUserRoleMapper;
import org.szy.plugin.sys.service.SysPermissionService;

import java.util.HashSet;
import java.util.Set;

import static org.szy.plugin.sys.entity.table.SysMenuTableDef.SYS_MENU;
import static org.szy.plugin.sys.entity.table.SysRoleMenuTableDef.SYS_ROLE_MENU;
import static org.szy.plugin.sys.entity.table.SysRoleTableDef.SYS_ROLE;
import static org.szy.plugin.sys.entity.table.SysUserRoleTableDef.SYS_USER_ROLE;

/**
 * 用户权限接口实现类
 *
 * @author 孙志阳
 * @date 2023-06-27
 * @since 1.0.0
 **/
@ProxyComponent
public class SysPermissionServiceImpl implements SysPermissionService {

    @Inject
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public Set<String> getRoles(Object userId) {
        if (ObjectUtil.isEmpty(userId)) return new HashSet<>();
        QueryWrapper qw = new QueryWrapper();
        qw.select(SYS_ROLE.CODE).from(SYS_ROLE)
                .innerJoin(SYS_USER_ROLE).on(SYS_ROLE.ID.eq(SYS_USER_ROLE.ROLE_ID))
                .and(SYS_ROLE.STATUS.eq(CommonStatusEnum.ENABLE.getKey()))
                .and(SYS_USER_ROLE.USER_ID.eq(userId));
        return new HashSet<>(sysUserRoleMapper.selectListByQueryAs(qw, String.class));
    }

    @Override
    public Set<String> getPermissions(Object userId) {
        if (ObjectUtil.isEmpty(userId)) return new HashSet<>();
        QueryWrapper qw = new QueryWrapper();
        qw.select(SYS_MENU.PERMISSION).from(SYS_MENU)
                .innerJoin(SYS_ROLE_MENU).on(SYS_MENU.ID.eq(SYS_ROLE_MENU.MENU_ID))
                .innerJoin(SYS_USER_ROLE).on(SYS_USER_ROLE.ROLE_ID.eq(SYS_ROLE_MENU.ROLE_ID))
                .innerJoin(SYS_ROLE).on(SYS_ROLE.ID.eq(SYS_ROLE_MENU.ROLE_ID))
                .and(SYS_ROLE.STATUS.eq(CommonStatusEnum.ENABLE.getKey()))
                .and(SYS_MENU.STATUS.eq(CommonStatusEnum.ENABLE.getKey())).and(SYS_MENU.TYPE.eq(2))
                .and(SYS_MENU.PERMISSION.isNotNull())
                .and(SYS_USER_ROLE.USER_ID.eq(userId))
                .and(SYS_USER_ROLE.USER_ID.isNotNull()).and(SYS_USER_ROLE.USER_ID.ne(StrUtil.EMPTY))
        ;
        return new HashSet<>(sysUserRoleMapper.selectListByQueryAs(qw, String.class));
    }

    @Override
    public Set<String> getPermissionsByRoleCode(String roleCodes) {
        if (StrUtil.isEmpty(roleCodes)) return new HashSet<>();
        QueryWrapper qw = new QueryWrapper();
        qw.select(SYS_MENU.PERMISSION).from(SYS_MENU)
                .innerJoin(SYS_ROLE_MENU).on(SYS_MENU.ID.eq(SYS_ROLE_MENU.MENU_ID))
                .innerJoin(SYS_ROLE).on(SYS_ROLE.ID.eq(SYS_ROLE_MENU.ROLE_ID))
                .and(SYS_ROLE.STATUS.eq(CommonStatusEnum.ENABLE.getKey()))
                .and(SYS_MENU.STATUS.eq(CommonStatusEnum.ENABLE.getKey())).and(SYS_MENU.TYPE.in(1, 2))
                .and(SYS_MENU.PERMISSION.isNotNull())
                .and(SYS_ROLE.CODE.in(StrUtil.join(",", roleCodes)))
                .and(SYS_ROLE.CODE.isNotNull()).and(SYS_ROLE.CODE.ne(StrUtil.EMPTY))
        ;
        return new HashSet<>(sysUserRoleMapper.selectListByQueryAs(qw, String.class));
    }
}
